package com.gin.servlet;

import com.gin.dao.*;
import com.gin.entity.*;
import com.gin.service.impl.JiaoshiServiceImpl;
import com.gin.service.impl.XueshengServiceImpl;

import com.gin.util.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *   找回密码
 */
@WebServlet(value = { "/forget.do" })
public class ForgetServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cx = "";
        String step = Request.get("step", "1");
        int sp = Integer.valueOf(step).intValue();

        String username = "";
        String daan = "";
        String xmm = "";
        Map user = null;
        if (sp > 1) {
            cx = Request.get("cx");
            username = Request.get("username");
            if (cx.equals("教师")) {
                user = Query.make("jiaoshi").where("gonghao", username).find();
            }
            if (cx.equals("学生")) {
                user = Query.make("xuesheng").where("xuehao", username).find();
            }
            if (user == null || user.isEmpty()) {
                assign("step", 1);
                assign("errorMsg", "没有找到该用户");
                view("/forget.jsp");
                return;
            }
            assign("user", user);
        }
        if (sp > 2) {
            daan = Request.get("daan");
            if (!daan.equals(user.get("daan"))) {
                assign("step", 2);
                assign("errorMsg", "没有找到该用户");
                view("/forget.jsp");
                return;
            }
        }
        if (sp > 3) {
            xmm = Request.get("xmm");
            if (cx.equals("教师")) {
                new JiaoshiServiceImpl().editPassword(user.get("id"), xmm);
            }
            if (cx.equals("学生")) {
                new XueshengServiceImpl().editPassword(user.get("id"), xmm);
            }
        }
        assign("step", step);
        view("/forget.jsp");
    }
}
